
/**
 * 行政老师查看报名某个实习岗位的学生列表
 * 
 * /xingzheng/internship/getStudentsByCompany
 */



const User_Info = require('../../../models/usersInfo')
const InternshipStudents = require('../../../models/internshipStudents')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Sequelize = require('sequelize')
const Op = Sequelize.Op

const get_list = async function (req, res) {
    try {
        InternshipStudents.belongsTo(User_Info, 
            {
                foreignKey: 'student_user_name', 
                targetKey: 'user_name'
            }
        )
    
        if (!req.query.company_id) {
            $.sendMessage(res, 1015, '参数 company_id 不存在')
            return
        }

        

        // findAndCountAll这个API既查找还统计满足条件的记录数
        var result =  await InternshipStudents.findAndCountAll({
            // attributes: ['id' ,'student_user_name', 'company_id', 'company_name', 'student_status', 'is_locked', 'teacher_user_fullname'], 
            where: {
                company_id: req.query.company_id
            },
            include: [
                { // include关键字表示关联查询
                    model: User_Info, // 指定关联的model
                    // as:'info', // 由于前面建立映射关系时为class表起了别名，那么这里也要与前面保持一致，否则会报错
                    // attributes: ['user_fullname' ,'user_major', 'user_class'], 
                }
            ],
            raw:true // 这个属性表示开启原生查询，原生查询支持的功能更多，自定义更强
        })

        res.json({
            code: 200,
            data: result
        })
        
        utils.create_a_log(req, '实习报名', '浏览报名某岗位的学生列表')
    }
    catch(error) {
        $.log('error :', error)
        res.json({
            code: 0000,
            message: '抓到异常',
            error: 'error' + error
        })
        utils.create_a_log(req, '实习报名', '浏览报名某岗位的学生列表', '失败')
    }
}
    
module.exports = get_list